package com.sense.kukoo.biz_media.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.biz.ChannelContentBean
import com.sense.kukoo.common.repo.http.bean.IndexContentBean
import com.sense.kukoo.common.repo.http.bean.UserInfoBean
import com.sense.kukoo.lib_logger.LoggerManager

class MediaChannelViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    private var mPageIndex = 2
    private val mPageSize = 20
    private var mIsRefreshData = true

    private var from_userId: Long = 0L

    val action_back: MutableLiveData<Boolean> = MutableLiveData()
    val action_more: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishRefres: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishLoadMore: MutableLiveData<Boolean> = MutableLiveData()
    val action_network_error: MutableLiveData<Boolean> = MutableLiveData()
    val data_channelContent: MutableLiveData<ArrayList<ChannelContentBean>> = MutableLiveData()
    val data_enableLoadMore:MutableLiveData<Boolean> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun setFromData(data: Long){
        from_userId = data
    }

    fun onBack(){
        action_back.postValue(true)
    }

    fun onMore(){
        action_more.postValue(true)
    }

    fun onRefreshData(){
        mIsRefreshData = true
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            action_network_error.postValue(true)
        }
        request(onError) {
            val data = arrayListOf<ChannelContentBean>()
            val user: UserInfoBean
            val isFollow:Boolean
            //
            val userInfo = Repo.http.getUserInfo(from_userId)
            if (userInfo.isSuccess()) {
                user = userInfo.data!!
            } else {
                LoggerManager.e(TAG, "获取用户信息失败")
                return@request
            }
            //
            if (Repo.login.isLogin()) {
                val subscription = Repo.http.isSubscription(from_userId)
                if (subscription.isSuccess()) {
                    isFollow = subscription.data!!
                } else {
                    LoggerManager.e(TAG, "获取用户信息失败")
                    return@request
                }
            } else {
                //未登陆的默认值
                isFollow = false
            }
            //
            data.add(ChannelContentBean(ChannelContentBean.TYPE_TITLE, user, null, isFollow))
            //
            val userContent = Repo.http.getUserContent(1, mPageSize, from_userId)
            if(userContent.isSuccess()){
                mPageIndex = 2
                val userContentData:List<IndexContentBean> = userContent.data!!
                if(userContentData.size == 0){
                    data.add(ChannelContentBean(ChannelContentBean.TYPE_ITEM_EMPTY, null, null))
                    data_enableLoadMore.postValue(false)
                } else {
                    userContentData.forEach {
                        data.add(ChannelContentBean(ChannelContentBean.TYPE_ITEM_NORMAL, null, it))
                    }
                    if(userContentData.size < mPageSize){
                        data_enableLoadMore.postValue(false)
                    } else {
                        data_enableLoadMore.postValue(true)
                    }
                }
            } else {
                Log.e(TAG, "result0=" + userContent.msg)
                userContent.msg.let { sendHintMessage(it) }
            }
            //
            action_finishRefres.postValue(true)
            data_channelContent.postValue(data)
        }
    }

    fun onLoadMoreData(){
        mIsRefreshData = false
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            action_network_error.postValue(true)
        }
        request(onError) {
            val data = data_channelContent.value!!

            val userContent = Repo.http.getUserContent(mPageIndex, mPageSize, from_userId)
            if(userContent.isSuccess()){
                mPageIndex++
                val userContentData:List<IndexContentBean> = userContent.data!!
                userContentData.forEach {
                    data.add(ChannelContentBean(ChannelContentBean.TYPE_ITEM_NORMAL, null, it))
                }
                //
                if(userContentData.size < mPageSize){
                    data_enableLoadMore.postValue(false)
                } else {
                    data_enableLoadMore.postValue(true)
                }

            } else {
                Log.e(TAG, "result0=" + userContent.msg)
                userContent.msg.let { sendHintMessage(it) }
            }
            //
            action_finishLoadMore.postValue(true)
            data_channelContent.postValue(data)
        }
    }

    fun onNetworkRetry(){
        if (mIsRefreshData) {
            onRefreshData()
        } else {
            onLoadMoreData()
        }
    }

    fun onFollowClick(mData: UserInfoBean){
        val data = data_channelContent.value!!
        var title:ChannelContentBean? = null
        data.forEach {
            if(it.type == ChannelContentBean.TYPE_TITLE){
                title = it
            }
        }
        title?.let { it ->
            val user = it.user!!
            val isFollow = it.isFollow

            toView(ACTION_SHOW_DIALOG)
            request {
                val subscription = Repo.http.subscription(user.id)
                if(subscription.isSuccess()){
                    title!!.isFollow = !isFollow
                    data_channelContent.postValue(data)
                } else {
                    Log.e(TAG, "result0=" + subscription.msg)
                    subscription.msg.let { sendHintMessage(it) }
                }
                toView(ACTION_HIDE_DIALOG)
            }
        }
    }
}